<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="inc/default :: default"></head>
<body style="background-color: #fff">
<form class="layui-form layer-pop" id="editForm" name="editForm" style="margin-left:10px;" th:object="${systemMenu}">
    <div class="layui-form-item layui-col-xs6">
        <label class="layui-form-label">上级菜单：</label>
        <div class="layui-input-block">
            <input type="hidden" name="pid" id="pid" th:value="*{pid}">
            <input type="text" name="pName" id="pName" readonly="readonly" class="layui-input" onclick="openMenuTreePage(this);" th:value="${pName}">
        </div>
    </div>
    <div class="layui-form-item layui-col-xs6">
        <label class="layui-form-label">排序号：</label>
        <div class="layui-input-block">
            <input type="text" name="sortNo" id="sortNo" autocomplete="off" class="layui-input" th:value="*{sortNo}">
        </div>
    </div>
    <div class="layui-form-item layui-col-xs6">
        <label class="layui-form-label">菜单名称：</label>
        <div class="layui-input-block">
            <input type="text" name="name" id="name" lay-verify="required" autocomplete="off" class="layui-input" th:value="*{name}">
        </div>
        <font style="color:red;float: right;margin-top: -30px;">*</font>
    </div>
    <div class="layui-form-item layui-col-xs6">
        <label class="layui-form-label">菜单地址：</label>
        <div class="layui-input-block">
            <input type="text" name="urlRequest" id="urlRequest" autocomplete="off" class="layui-input" th:value="*{urlRequest}">
        </div>
    </div>
    <div class="layui-form-item layui-col-xs6">
        <label class="layui-form-label">图标配置：</label>
        <div class="layui-input-block">
            <input type="text" name="menuStyle" id="menuStyle" autocomplete="off" class="layui-input" th:value="*{menuStyle}">
        </div>
    </div>
    <div class="layui-form-item layui-col-xs6">
        <label class="layui-form-label">状态：</label>
        <div class="layui-input-block">
            <input type="checkbox" id="status" name="status" lay-skin="switch" lay-text="启用|禁用">
        </div>
    </div>
    <div class="layui-form-item layui-col-xs12">
        <label class="layui-form-label">备注：</label>
        <div class="layui-input-block">
            <textarea id="note" name="note" autocomplete="off" class="layui-textarea" th:text="*{note}"></textarea>
        </div>
    </div>
    <input type="hidden" name="menuId" id="menuId" th:value="*{menuId}">
    <button type="submit" class="layui-btn" lay-submit lay-filter="submit1" style="display:none;"></button>
</form>
</body>
<script type="text/javascript" th:inline="javascript">
    /*<![CDATA[*/
    var ctxPath = /*[[@{/}]]*/ '';
    /*]]>*/
    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引

    ;!function(){
        var layer1 = layui.layer
            ,form = layui.form;

        var type = [[${type}]];
        if ("update" == type) {
            if ([[${systemMenu.status}]]) {
                $('#status').attr('checked', true);
            }
        } else {
            $('#status').attr('checked', true);
        }
        form.render();

        form.on('submit(submit1)', function(data){
            var type = [[${type}]];
            var msg = "新增";
            if ("update" == type) {
                msg = "修改";
            }
            $.ajax({
                type: 'post',
                url : ctxPath + 'systemMenu/' + [[${type}]],
                data : data.field,
                success : function(result) {
                    if (result) {
                        layer1.msg(msg + "菜单成功！", {
                            icon : 1,
                            time : 2000
                        }, function() {
                            if (index) {
                                parent.layer.close(index);
                            }
                            parent.tableIns.reload();
                            parent.treeObj.reAsyncChildNodes(null, "refresh");
                        });
                    } else {
                        layer1.msg(msg + "菜单失败！", {
                            icon : 2,
                            anim : 6,
                            time : 2000
                        });
                    }
                },
                error : function() {
                    layer1.msg(msg + "菜单失败！", {
                        icon : 2,
                        anim : 6,
                        time : 2000
                    });
                }
            });
            return false;
        });
        //菜单图标
        $("#menuStyle").iconfont();
    }();

    function openMenuTreePage(obj) {
        layer.open({
            type : 1,
            title : '选择菜单',
            maxmin : true,
            shadeClose : false,
            area : [ '350px', '400px' ],
            btn : ['确定', '取消'],
            content : '<ul id="menuTree" class="ztree"></ul>',
            success : function(layero, index) {
                var setting = {
                    async : {
                        enable : true,
                        url : ctxPath + 'systemMenu/menuList',
                    },
                    data : {
                        simpleData : {
                            enable : true,
                            idKey : "menuId",
                            pIdKey : "pid"
                        }
                    },
                    callback : {
                        onAsyncSuccess : function() {
                            var treeObj = $.fn.zTree.getZTreeObj("menuTree");
                            treeObj.expandAll(true);
                        }
                    }
                };
                $.fn.zTree.init($("#menuTree"), setting);
            },
            yes: function(index,layero){
                var treeObj = $.fn.zTree.getZTreeObj("menuTree");
                var node = treeObj.getSelectedNodes()[0];
                $(obj).attr("data", node.menuId);
                $(obj).val(node.name);
                $("#pid").val(node.menuId);
                layer.close(index);
            }
        });
    }
</script>